home *** CD-ROM | disk | FTP | other *** search
/ hornet.scene.org / hornet.scene.org FTP 11-25-2012.zip / hornet.scene.org FTP 11-25-2012 / code / 3d / trifill / texmap / bastxmap.txt < prev    next >
Text File  |  2012-06-16  |  9KB  |  282 lines

  1. Newsgroups: comp.graphics.algorithms
  2. Path: usenet.ee.pdx.edu!cs.uoregon.edu!sgiblab!swrinde!cs.utexas.edu!uunet!pipex!uknet!dmu.ac.uk!se1ap
  3. From: se1ap@dmu.ac.uk (Alan Parker)
  4. Subject: Texture Mapping again!
  5. Message-ID: <1994Feb7.115343.13939@dmu.ac.uk>
  6. Sender: news@dmu.ac.uk (Usenet News Management)
  7. Nntp-Posting-Host: aether.cms.dmu.ac.uk
  8. Organization: De Montfort University, Leicester, UK
  9. Date: Mon, 7 Feb 1994 11:53:43 GMT
  10. Lines: 269
  11.  
  12. '
  13. ' Texture Mapping example.
  14. ' Maps 4 sided picture onto a 4 sided polygon.
  15. '
  16. ' Written in GFA basic version 2
  17. '
  18. ' By Alan Parker (E-Mail: se1ap@dmu.ac.uk)
  19. ' On 1/2/94
  20. '
  21. ' This texture mapping method was 'borrowed' from Ben of Chaos.
  22. ' I've no idea who originally thought of this method.
  23. '
  24. ' There's not many detailed comments, I've already done my best when I
  25. ' explained the algorithm before and nobody understood it, now it's up to you!
  26. '
  27. ' Note: All variables with % after them are Integers, all other are Reals.
  28. '       Y co-ords go from top(0) to bottom(199) of the screen.
  29. '       'picture' refers to the original bitmapped picture.
  30. '       'polygon' refers to the polygon drawn on screen.
  31. '       The picture to be mapped must be in the top,left (0,0) corner of the
  32. '       screen.
  33. '       This is not a perspective map, but you would only have to modify the
  34. '       scan convertor to generate the picture x,y points depending on the
  35. '       z co-ords of the polygon. The texture mapping routine would stay the
  36. '       same.
  37. '       I've half managed to do a perspective map, but it's still bugged!
  38. '       It will be posted when (if?) I manage to get it working...
  39. ' *********************************************
  40. ' ST specific code.
  41. ' Find screen address
  42. '
  43. Screen_base%=Xbios(2)               !ST specific call, get screen base address
  44. '
  45. ' Set screen address and set to low resolution
  46. Void Xbios(5,L:Screen_base%,L:Screen_base%,W:0)
  47. '
  48. ' Load in the picture to be texture mapped (must be in top,left of screen)
  49. Bload "d:\texturem.dem\tmap_pic.pi1",Screen_base%-34
  50. '
  51. ' *********************************************
  52. ' General Texture Mapping code.
  53. ' Machine independent, probably :-) 
  54. '
  55. ' Variables and arrays.
  56. '
  57. Dim Left_table%(400,2),Right_table%(400,2)  ! Scan converter tables for polygon
  58. '
  59. ' p.s - replace both the 400 values above with your max screen height in pixels
  60. '
  61. Dim Poly_points(3,1) ! Array for polygon co-ords, 4 pairs(x,y) co-ords
  62. '
  63. P_width%=96     !original picture width in pixels
  64. P_height%=96    !original picture height in pixels
  65. '
  66. Min_y%=32767    !set initial smallest y co-ord of polygon after rotation
  67. Max_y%=0        !set initial largest y co-ord of polygon after rotation
  68. '
  69. ' Main program
  70. '
  71. @Get_polygon_points  !get initial polygon points from DATA statements into array
  72. @Find_small_large_y  !determine the smallest and the largest y-coord in the poly
  73. '
  74. ' Send polygon points to the scan converter
  75. X1%=Poly_points(0,0)
  76. Y1%=Poly_points(0,1)
  77. X2%=Poly_points(1,0)
  78. Y2%=Poly_points(1,1)
  79. @Scan_convert(X1%,Y1%,X2%,Y2%,"top")    !scan top of picture
  80. X1%=Poly_points(2,0)
  81. Y1%=Poly_points(2,1)
  82. @Scan_convert(X2%,Y2%,X1%,Y1%,"right")  !scan right of picture
  83. X2%=Poly_points(3,0)
  84. Y2%=Poly_points(3,1)
  85. @Scan_convert(X1%,Y1%,X2%,Y2%,"bottom") !scan bottom of picture
  86. X1%=Poly_points(0,0)
  87. Y1%=Poly_points(0,1)
  88. @Scan_convert(X2%,Y2%,X1%,Y1%,"left")   !scan left of picture
  89. '
  90. ' Do the actual texture mapping
  91. @Texture_map
  92. End
  93. '
  94. ' *************************************************************
  95. '
  96. ' Procedures
  97. '
  98. Procedure Get_polygon_points
  99.   Restore Polygon_points        ! start of un-rotated polygon co-ords
  100.   '
  101.   For Count%=0 To 3
  102.     Read Poly_points(Count%,0)  ! read x xo-ord
  103.     Read Poly_points(Count%,1)  ! read y co-ord
  104.   Next Count%
  105. Return
  106. '
  107. Procedure Find_small_large_y
  108.   For Count%=0 To 3
  109.     Y_coord%=Poly_points(Count%,1)
  110.     '
  111.     If Y_coord%<Min_y%   ! is this the new lowest y co-ord?
  112.       Min_y%=Y_coord%    ! Yes...
  113.     Endif
  114.     '
  115.     If Y_coord%>Max_y%   ! is this the new highest y co-ord?
  116.       Max_y%=Y_coord%    ! Yes...
  117.     Endif
  118.   Next Count%
  119. Return
  120. '
  121. ' This is the actual mapping routine.
  122. ' It takes the co-ords that have been calcualted by the scan converter
  123. ' and 'traces' accross the original picture in between them looking at the
  124. ' pixel colour and then plotting a pixel in that colour in the current position
  125. ' within the polygon.
  126. '
  127. Procedure Texture_map
  128.   '
  129.   For Y%=Min_y% To Max_y%
  130.     Poly_x1%=Left_table%(Y%,0)  !get left polygon x
  131.     P_x1=Left_table%(Y%,1)      !get left picture x
  132.     P_y1=Left_table%(Y%,2)      !get left picture y
  133.     '
  134.     Poly_x2%=Right_table%(Y%,0) !get right polygon x
  135.     P_x2=Right_table%(Y%,1)     !get right picture x
  136.     P_y2=Right_table%(Y%,2)     !get right picture y
  137.     Line_width%=Poly_x2%-Poly_x1%  !what is the width of this polygon line
  138.     Inc Line_width%  !QUICK fix so it doesn't do divide by zero
  139.     '
  140.     Px_add=(P_x2-P_x1)/Line_width%  !'squash' picture x_dist into polygon x_dist
  141.     Py_add=(P_y2-P_y1)/Line_width%  !'squash' picture y_dist into polygon x_dist
  142.     '
  143.     For X%=Poly_x1% To Poly_x2%
  144.       Col%=Point(P_x1,P_y1)   ! get colour of pixel at current pos in picture
  145.       '
  146.       Color Col%              ! set draw colour to the determined colour
  147.       Plot X%,Y%              ! plot the pixel in the correct colour
  148.       Add P_x1,Px_add         ! move x picture co-ord
  149.       Add P_y1,Py_add         ! move y picture co-ord
  150.     Next X%
  151.     '
  152.   Next Y%
  153. Return
  154. '
  155. ' This procedure calculates the x points for the left side of the polygon
  156. ' It also calculates the x,y co-ords of the picture for the left side of the
  157. ' polygon.
  158. '
  159. Procedure Scan_left_side(X1%,X2%,Y_top%,Line_height%,P_side$)
  160.   Inc Line_height%  ! No divide by zero
  161.   X_add=(X2%-X1%)/Line_height%
  162.   '
  163.   If P_side$="top"
  164.     Px=P_width%
  165.     Py=0
  166.     Px_add=-P_width%/Line_height%
  167.     Py_add=0
  168.   Endif
  169.   If P_side$="right"
  170.     Px=P_width%
  171.     Py=P_height%
  172.     Px_add=0
  173.     Py_add=-P_height%/Line_height%
  174.   Endif
  175.   If P_side$="bottom"
  176.     Px=0
  177.     Py=P_height%
  178.     Px_add=P_width%/Line_height%
  179.     Py_add=0
  180.   Endif
  181.   If P_side$="left"
  182.     Px=0
  183.     Py=0
  184.     Px_add=0
  185.     Py_add=P_height%/Line_height%
  186.   Endif
  187.   '
  188.   X=X1%
  189.   For Y%=0 To Line_height%
  190.     Left_table%(Y_top%+Y%,0)=X    !polygon x
  191.     Left_table%(Y_top%+Y%,1)=Px   !picture x
  192.     Left_table%(Y_top%+Y%,2)=Py   !picture y
  193.     Add X,X_add                   !Next polygon x
  194.     Add Px,Px_add                 !Next picture x
  195.     Add Py,Py_add                 !Next picture y
  196.   Next Y%
  197. Return
  198. '
  199. ' This procedure calculates the x points for the right side of the polygon
  200. ' It also calculates the x,y co-ords of the picture for the right side of the
  201. ' polygon.
  202. '
  203. Procedure Scan_right_side(X1%,X2%,Y_top%,Line_height%,P_side$)
  204.   Inc Line_height%           ! No divide by zero
  205.   X_add=(X2%-X1%)/Line_height%
  206.   '
  207.   If P_side$="top"
  208.     Px=0
  209.     Py=0
  210.     Px_add=P_width%/Line_height%
  211.     Py_add=0
  212.   Endif
  213.   If P_side$="right"
  214.     Px=P_width%
  215.     Py=0
  216.     Px_add=0
  217.     Py_add=P_height%/Line_height%
  218.   Endif
  219.   If P_side$="bottom"
  220.     Px=P_width%
  221.     Py=P_height%
  222.     Px_add=-P_width%/Line_height%
  223.     Py_add=0
  224.   Endif
  225.   If P_side$="left"
  226.     Px=0
  227.     Py=P_height%
  228.     Px_add=0
  229.     Py_add=-P_height%/Line_height%
  230.   Endif
  231.   X=X1%
  232.   For Y%=0 To Line_height%
  233.     Right_table%(Y_top%+Y%,0)=X   !polygon x
  234.     Right_table%(Y_top%+Y%,1)=Px  !picture x
  235.     Right_table%(Y_top%+Y%,2)=Py  !picture y
  236.     Add X,X_add                   !Next polygon x
  237.     Add Px,Px_add                 !Next picture x
  238.     Add Py,Py_add                 !Next picture y
  239.   Next Y%
  240. Return
  241. '
  242. ' The procedure takes lines as defined by x1,y1,x2,y2.
  243. ' It also takes a string telling it which side of the picture we are mapping
  244. ' The strings are top,right,bottom,left.
  245. ' This routine decides which 'side' of the polygon the line is on, and then
  246. ' calls the apropriate routine.
  247. '
  248. Procedure Scan_convert(X1%,Y1%,X2%,Y2%,P_side$)
  249.   If Y2%<Y1%
  250.     Swap X1%,X2%  ! swap these variable round so we always scan from top to...
  251.     Swap Y1%,Y2%  ! ... bottom
  252.     Line_height%=Y2%-Y1%
  253.     @Scan_left_side(X1%,X2%,Y1%,Line_height%,P_side$)
  254.   Else
  255.     Line_height%=Y2%-Y1%
  256.     @Scan_right_side(X1%,X2%,Y1%,Line_height%,P_side$)
  257.   Endif
  258. Return
  259. '
  260. ' The points for the polygon.
  261. ' These points in the form x,y define the shape of a four sided polygon
  262. ' rotated 45 degrees. These are 2d points that would normally come from a 3d
  263. ' routine after perspective had been applied.
  264. ' The points must be defines clockwise....
  265. '
  266. Polygon_points:
  267. Data 100,100
  268. Data 150,150
  269. Data 100,200
  270. Data 50,150
  271. '
  272. ' an alternative polygon to try
  273. '
  274. ' Polygon_points:
  275. Data 100,100
  276. Data 150,100
  277. Data 200,100
  278. Data 50,100
  279.  
  280.  
  281.  
  282.